/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cong.javadanang.model.dao.impl;

import cong.javadanang.model.dao.CommentDAO;
import cong.javadanang.model.entity.Comment;
import cong.javadanang.model.entity.Comment;
import hibernate.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Pham
 */
public class CommentDAOImpl implements CommentDAO {

    private static CommentDAOImpl commentDAO;

    public static CommentDAOImpl getInstance() {
        if (commentDAO == null) {
            commentDAO = new CommentDAOImpl();
        }
        return commentDAO;
    }
    private Session session;
    private Transaction ts;

    @Override
    public Comment getCommentById(int commentId) {
        Comment comment = null;
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            comment = (Comment) session.get(Comment.class, commentId);
            ts.commit();
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
        }finally{
            session.close();
        }
        return comment;
    }

    @Override
    public List<Comment> getAllComments() {
        session = HibernateUtil.getSessionFactory().openSession();
        List<Comment> comments = null;
        try {
            ts = session.beginTransaction();
            String sql = "FROM Comment";
            Query query = session.createQuery(sql);
            comments = query.list();
            ts.commit();
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
        }finally{
            session.close();
        }
        return comments;
    }

    @Override
    public boolean createComment(Comment comment) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.save(comment);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean updateComment(Comment comment) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.update(comment);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean deleteComment(Comment comment) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.delete(comment);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean removeComment(Comment comment) {
        session = HibernateUtil.getSessionFactory().openSession();
         try {
            ts = session.beginTransaction();
            String sql = "UPDATE Comment set active = false WHERE commentId = :commentId";
            Query query = session.createQuery(sql);
            query.setParameter("commentId", comment.getCommentId());
            query.executeUpdate();
            ts.commit();
            return true;
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean restoreComment(Comment comment) {
        session = HibernateUtil.getSessionFactory().openSession();
         try {
            ts = session.beginTransaction();
            String sql = "UPDATE Comment set active = true WHERE commentId = :commentId";
            Query query = session.createQuery(sql);
            query.setParameter("commentId", comment.getCommentId());
            query.executeUpdate();
            ts.commit();
            return true;
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

}
